#include "CalibTab.h"

#ifndef ROOT_TGLabel
#include <TGLabel.h>
#endif

#ifndef ROOT_TGButton
#include <TGButton.h>
#endif

CalibTab::CalibTab(void)
{
    m_smallCalib.resize(6);
    m_smallCalib[0].second = "Radius (in cm)";
    m_smallCalib[1].second = "Temperature coefficient";
    m_smallCalib[2].second = "Density coefficient";
    m_smallCalib[3].second = "Energy coefficient";
    m_smallCalib[4].second = "Angle (degrees)";
    m_smallCalib[5].second = "Distance axis-laser ray (in cm)";

    m_backgroundCalib.resize(5);
    m_spectralCalib.resize(5);
    m_peakCalib.resize(5);
}


CalibTab::~CalibTab(void)
{
    for( int i = 0; i < m_smallCalib.size(); ++i )
            delete m_smallCalib[i].first;

    for( int i = 0; i < m_backgroundCalib.size(); ++i )
    {
            delete m_backgroundCalib[i].first;
            delete m_backgroundCalib[i].second;
    }

    for( int i = 0; i < m_peakCalib.size(); ++i )
    {
            delete m_peakCalib[i].first;
            delete m_peakCalib[i].second;
    }

    for( int i = 0; i < m_spectralCalib.size(); ++i )
    {
            for( int j = 0; j < m_spectralCalib[i].size(); ++j )
                    delete m_spectralCalib[i][j];
    }

    delete m_archiveName;
}

void CalibTab::ReadCalibrations(int shotNum)
{
    TString str = m_archiveName->GetDisplayText();
    if( str.Length() == 0 )
            return;

    ArchiveWrapper arc( std::string( str.Data() ), shotNum );
    if( arc.Valid() )
    {
        CalibData calibrations;
        if( arc.GetCalibrations( calibrations ) )
            ShowCalibrations( calibrations );
    }
}

void CalibTab::Create( TGTab *fDataTabCalibTab, void* appPtr, std::string arcName )
{
    // container of "Calib Tab"
   TGCompositeFrame *fCalibTabFrame;
   fCalibTabFrame = fDataTabCalibTab->AddTab("Calibration Tab");
   fCalibTabFrame->SetLayoutManager(new TGVerticalLayout(fCalibTabFrame));
   fCalibTabFrame->SetLayoutBroken(kTRUE);

   TGLabel *fCalibTabArchieveLabel = new TGLabel(fCalibTabFrame,"archivename" );
   fCalibTabFrame->AddFrame(fCalibTabArchieveLabel, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
   fCalibTabArchieveLabel->MoveResize(16,24,112,24);

   m_archiveName = new TGTextEntry(fCalibTabFrame, new TGTextBuffer(15) );
   fCalibTabFrame->AddFrame(m_archiveName, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
   m_archiveName->MoveResize(136,24,600,22);
   m_archiveName->SetEnabled(false);
   m_archiveName->SetText(arcName.c_str());

   TGTextButton *readCalibsButton = new TGTextButton(fCalibTabFrame,"Read callibrations");
   readCalibsButton->MoveResize(128,56,120,24);
   fCalibTabFrame->AddFrame(readCalibsButton, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
   readCalibsButton->Connect("Pressed()",  "AppGUI", appPtr, "InvokeReadCalibrations()");

   TGTextButton *saveCalibsButton = new TGTextButton(fCalibTabFrame,"Save all callibrations");
   saveCalibsButton->MoveResize(256,56,140,24);
   fCalibTabFrame->AddFrame(saveCalibsButton, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
   saveCalibsButton->Connect("Pressed()",  "AppGUI", appPtr, "SaveCalibs()");
   
   CreateSmallCalib(fCalibTabFrame);
   CreateBgCalib(fCalibTabFrame);
   CreateSpecCalib(fCalibTabFrame);
   CreatePeakCalib(fCalibTabFrame);

//   ReadCalibrations();
}

void CalibTab::CreateSmallCalib(TGCompositeFrame *fCalibTabFrame)
{
	// "Small callibrations" group frame
	TGGroupFrame *fSmallCalibGroup = new TGGroupFrame(fCalibTabFrame,"Small callibrations",kVerticalFrame | kRaisedFrame);
	fSmallCalibGroup->SetLayoutBroken(kTRUE);

	int yPos = 24;
	int smallStep = 28;
	int bigStep = 32;
	for( int i = 0; i < m_smallCalib.size(); ++i )
	{
		TGLabel *label = new TGLabel(fSmallCalibGroup, m_smallCalib[i].second);
		fSmallCalibGroup->AddFrame(label, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
		label->MoveResize(20, yPos, 0, 24);

		yPos += smallStep;

		m_smallCalib[i].first = new TGTextEntry(fSmallCalibGroup, new TGTextBuffer(15));
		fSmallCalibGroup->AddFrame(m_smallCalib[i].first, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
		m_smallCalib[i].first->MoveResize(40, yPos, 120, 22);

		yPos += bigStep;
	}

	fSmallCalibGroup->SetLayoutManager(new TGVerticalLayout(fSmallCalibGroup));
	fCalibTabFrame->AddFrame(fSmallCalibGroup, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
	fSmallCalibGroup->MoveResize(8,128,200,424);
}

void CalibTab::CreateBgCalib(TGCompositeFrame *fCalibTabFrame)
{
    // "Fon callibrations" group frame
	TGGroupFrame *fCalibTabFonCalibGroup = new TGGroupFrame(fCalibTabFrame,"Backgroud callibrations",kVerticalFrame);
	fCalibTabFonCalibGroup->SetLayoutBroken(kTRUE);

	char name[256];
	int yPos = 64;
	int smallStep = 24;
	int bigStep = 32;
	for(int i = 0; i < m_backgroundCalib.size(); ++i)
	{
		sprintf(name, "Chanel %d\0", i+1);
		TGLabel *label = new TGLabel(fCalibTabFonCalibGroup, name );
		fCalibTabFonCalibGroup->AddFrame(label, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
		label->MoveResize(40, yPos, 112, 18);

		yPos += smallStep;

		m_backgroundCalib[i].first = new TGTextEntry(fCalibTabFonCalibGroup, new TGTextBuffer(15));
		fCalibTabFonCalibGroup->AddFrame(m_backgroundCalib[i].first, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
		m_backgroundCalib[i].first->MoveResize(8, yPos, 80, 22);

		m_backgroundCalib[i].second = new TGTextEntry(fCalibTabFonCalibGroup, new TGTextBuffer(15));
		fCalibTabFonCalibGroup->AddFrame(m_backgroundCalib[i].second, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
		m_backgroundCalib[i].second->MoveResize(104, yPos, 80, 22);

		yPos += bigStep;
	}
   
	fCalibTabFonCalibGroup->SetLayoutManager(new TGVerticalLayout(fCalibTabFonCalibGroup));
	fCalibTabFrame->AddFrame(fCalibTabFonCalibGroup, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
	fCalibTabFonCalibGroup->MoveResize(224,128,200,424);
}

void CalibTab::CreateSpecCalib(TGCompositeFrame *fCalibTabFrame)
{
// "Spectral callibration" group frame
	TGGroupFrame *fCalibTabSpecGroup = new TGGroupFrame(fCalibTabFrame,"Spectral callibration",kVerticalFrame | kRaisedFrame);
	fCalibTabSpecGroup->SetLayoutBroken(kTRUE);

	TGLabel *fCalibTabSpecLabel = new TGLabel(fCalibTabSpecGroup,"Spectral line/ Spectral coeff/ error" );
	fCalibTabSpecGroup->AddFrame(fCalibTabSpecLabel, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
	fCalibTabSpecLabel->MoveResize(16,32,224,18);
   
	char name[256];
	int yPos = 64;
	int smallStep = 24;
	int bigStep = 32;

	for( int i = 0; i < m_spectralCalib.size(); ++i )
	{
		m_spectralCalib[i].resize(3);

		sprintf(name, "Chanel %d\0", i+1);

		TGLabel *label = new TGLabel(fCalibTabSpecGroup, name );
		fCalibTabSpecGroup->AddFrame(label, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
		label->MoveResize(16, yPos, 112, 18);

		yPos += smallStep;

		m_spectralCalib[i][0] = new TGTextEntry(fCalibTabSpecGroup, new TGTextBuffer(15) );
		fCalibTabSpecGroup->AddFrame(m_spectralCalib[i][0], new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
		m_spectralCalib[i][0]->MoveResize(16, yPos, 71, 22);

		m_spectralCalib[i][1] = new TGTextEntry(fCalibTabSpecGroup, new TGTextBuffer(15) );
		fCalibTabSpecGroup->AddFrame(m_spectralCalib[i][1], new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
		m_spectralCalib[i][1]->MoveResize(96, yPos, 71, 22);

		m_spectralCalib[i][2] = new TGTextEntry(fCalibTabSpecGroup, new TGTextBuffer(15) );
		fCalibTabSpecGroup->AddFrame(m_spectralCalib[i][2], new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
		m_spectralCalib[i][2]->MoveResize(168, yPos, 71, 22);

		yPos += bigStep;
	}

	fCalibTabSpecGroup->SetLayoutManager(new TGVerticalLayout(fCalibTabSpecGroup));
	fCalibTabFrame->AddFrame(fCalibTabSpecGroup, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
	fCalibTabSpecGroup->MoveResize(440,128,248,424);
}

void CalibTab::CreatePeakCalib(TGCompositeFrame *fCalibTabFrame)
{
// "Peak ratio" group frame
	TGGroupFrame *fCalibTabPeakRatioGroup = new TGGroupFrame(fCalibTabFrame,"Peak ratio",kVerticalFrame);
	fCalibTabPeakRatioGroup->SetLayoutBroken(kTRUE);

   	char name[256];
	int yPos = 64;
	int smallStep = 24;
	int bigStep = 32;
	for(int i = 0; i < m_peakCalib.size(); ++i)
	{
		sprintf(name, "Chanel %d\0", i+1);
		TGLabel *label = new TGLabel(fCalibTabPeakRatioGroup, name );
		fCalibTabPeakRatioGroup->AddFrame(label, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
		label->MoveResize(40, yPos, 112, 18);

		yPos += smallStep;

		m_peakCalib[i].first = new TGTextEntry(fCalibTabPeakRatioGroup, new TGTextBuffer(15));
		fCalibTabPeakRatioGroup->AddFrame(m_peakCalib[i].first, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
		m_peakCalib[i].first->MoveResize(8, yPos, 80, 22);

		m_peakCalib[i].second = new TGTextEntry(fCalibTabPeakRatioGroup, new TGTextBuffer(15));
		fCalibTabPeakRatioGroup->AddFrame(m_peakCalib[i].second, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
		m_peakCalib[i].second->MoveResize(104, yPos, 80, 22);

		yPos += bigStep;
	}

	fCalibTabPeakRatioGroup->SetLayoutManager(new TGVerticalLayout(fCalibTabPeakRatioGroup));
	fCalibTabFrame->AddFrame(fCalibTabPeakRatioGroup, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
	fCalibTabPeakRatioGroup->MoveResize(704,128,208,424);
}

void CalibTab::GetSmallCalib( SmallCalibData &smallCalib )
{
/*	this is done in constructor
	m_smallCalib[0].second = "Radius";
	m_smallCalib[1].second = "Temperature coefficient";
	m_smallCalib[2].second = "Density coefficient";
	m_smallCalib[3].second = "Energy coefficient";
	m_smallCalib[4].second = "Angle (degrees)";
	m_smallCalib[5].second = "Distance axis-laser ray ( in cm)";*/

	smallCalib.radius = (double)(m_smallCalib[0].first->GetDisplayText().Atof());
	smallCalib.temperatureCoef = (double)(m_smallCalib[1].first->GetDisplayText().Atof());
	smallCalib.densityCoef = (double)(m_smallCalib[2].first->GetDisplayText().Atof());
	smallCalib.energyCoef = (double)(m_smallCalib[3].first->GetDisplayText().Atof());
	smallCalib.angle = (double)(m_smallCalib[4].first->GetDisplayText().Atof());
	smallCalib.distanceAxisLaserRay = (double)(m_smallCalib[5].first->GetDisplayText().Atof());
	
}

void CalibTab::GetBgCalib( BgCalibData &bgCalib )
{
	for( int i = 0; i < m_backgroundCalib.size(); ++i )
	{
		bgCalib[i].val = (double)(m_backgroundCalib[i].first->GetDisplayText().Atof());
		bgCalib[i].error = (double)(m_backgroundCalib[i].second->GetDisplayText().Atof());
	}
}

void CalibTab::GetSpecCalib( SpectralCalibData &specCalib )
{
	for( int i = 0; i < m_spectralCalib.size(); ++i )
	{
		specCalib.specLine[i] = (double)(m_spectralCalib[i][0]->GetDisplayText().Atof());
		specCalib.specCoef[i].val = (double)(m_spectralCalib[i][1]->GetDisplayText().Atof());
		specCalib.specCoef[i].error = (double)(m_spectralCalib[i][2]->GetDisplayText().Atof());
	}
}
void CalibTab::GetPeakCalib( PeakRatioCalibData &peakCalib )
{
	for( int i = 0; i < m_peakCalib.size(); ++i )
	{
		peakCalib[i].val = (double)(m_peakCalib[i].first->GetDisplayText().Atof());
		peakCalib[i].error = (double)(m_peakCalib[i].second->GetDisplayText().Atof());
	}
}

void CalibTab::ShowCalibrations( CalibData &calibrations)
{
	char str[256];
	sprintf(str, "%.3f\0", calibrations.smallCalib.radius);
	m_smallCalib[0].first->SetText( str );

	sprintf(str, "%.3f\0", calibrations.smallCalib.temperatureCoef);
	m_smallCalib[1].first->SetText( str );

	sprintf(str, "%.3f\0", calibrations.smallCalib.densityCoef);
	m_smallCalib[2].first->SetText( str );

	sprintf(str, "%.3f\0", calibrations.smallCalib.energyCoef);
	m_smallCalib[3].first->SetText( str );

	sprintf(str, "%.3f\0", calibrations.smallCalib.angle);
	m_smallCalib[4].first->SetText( str );

	sprintf(str, "%.3f\0", calibrations.smallCalib.distanceAxisLaserRay);
	m_smallCalib[5].first->SetText( str );

	for( int i = 0; i < m_backgroundCalib.size(); ++i )
	{
		sprintf(str, "%.3f\0", calibrations.bgCalib[i].val);
		m_backgroundCalib[i].first->SetText(str);

		sprintf(str, "%.3f\0", calibrations.bgCalib[i].error);
		m_backgroundCalib[i].second->SetText(str);
	}

	for( int i = 0; i < m_peakCalib.size(); ++i )
	{
		sprintf(str, "%.3f\0", calibrations.peakRatioCalib[i].val);
		m_peakCalib[i].first->SetText(str);

		sprintf(str, "%.3f\0", calibrations.peakRatioCalib[i].error);
		m_peakCalib[i].second->SetText(str);
	}

	for( int i = 0; i < m_spectralCalib.size(); ++i )
	{
		sprintf(str, "%.3f\0", calibrations.spectralCalib.specLine[i]);
		m_spectralCalib[i][0]->SetText(str);

		sprintf(str, "%.3f\0", calibrations.spectralCalib.specCoef[i].val);
		m_spectralCalib[i][1]->SetText(str);

		sprintf(str, "%.3f\0", calibrations.spectralCalib.specCoef[i].error);
		m_spectralCalib[i][2]->SetText(str);
	}
}
